<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../common/taglibs.jsp" %>
<%
    response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<jsp:include page="/page/common/include.jsp"></jsp:include>
<script language="javascript" type="text/javascript">

$(document).ready(function(){
	
	/** 初始化查询面板 */
	$("#searchPanel").panel({
		collapsible:true,
		border:false,
		closable:true
	});
	
	/** 初始化编辑页面 */
	$("#editDiv").window({
		title:'修改角色信息',
		width:500,
		height:400,
		modal:true,
		closed:true,
		minimizable:false,
		maximizable:false
	});
	
	/** 初始化查看页面 */
	$("#viewDiv").window({
		title:'查看角色信息',
		width:550,
		height:420,
		modal:true,
		closed:true,
		minimizable:false,
		maximizable:false
	});
	
	/** 初始化角色信息Grid Table */
	$("#gridTable").datagrid({
		title:"角色信息列表",
		url:'<%=request.getContextPath()%>/roleInfo/roleInfoAction!listRoleInfo.do',
		singleSelect:true,
		rownumbers:true,
		striped:true,
		fitColumns:true,
		toolbar:"#toolbar",
		pagination:true,
		columns:[[
       		{field:'roleId',title:'角色ID',width:100},
          	{field:'roleName',title:'角色名称',width:100},
          	{field:'roleDesc',title:'角色描述',width:100,align:'right'},
          	{field:'roleStatus',title:'有效状态',width:100,align:'right',formatter:statusFormat},
          	{field:'version',title:'版本号',width:100}
     	]],
     	onClickRow:function(rowIndex,rowData){
     		
     	},
     	onBeforeLoad:function(){
     		disableLinkButton();
     	},
		onLoadSuccess:function(){
			enableLinkButton();
		}
	});
	
});

/**
 * 格式化用户信息状态
 */
function statusFormat(value,rowData){
	if(value == '0'){
		return '<span style="color:green;">启用</span>';
	}else if(value == '1'){
		return '<span style="color:red;">停用</span>';
	}
}

/**
 * 角色信息查询
 */
function roleInfoSearch(){
	$("#roleName_search").val($.trim($("#roleName_search").val()));
	$("#roleDesc_search").val($.trim($("#roleDesc_search").val()));
	$("#gridTable").datagrid("load",{
		roleName:$("#roleName_search").val(),
		roleDesc:$("#roleDesc_search").val()
	});
}

/**
 * 清空查询条件
 */
function clearSearchCriteria(){
	$("#roleName_search").val('');
	$("#roleDesc_search").val('');
	goToPage('<%=request.getContextPath()%>/roleInfo/roleInfoAction!initManageRoleInfo.do');
}

/**
 * 修改角色信息
 */
function edit(){
	var rowData = $('#gridTable').datagrid('getSelected');
	if(rowData == null){
		messageAlertWarning("操作提示","请选择要修改的角色信息！");
		return;
	}
	$.ajax({
		url : '<%=request.getContextPath()%>/roleInfo/roleInfoAction!initEditRoleInfo.do',
		type : 'post',
		dataType : 'text',
		cache : false,
		data : {
			roleId : rowData.roleId,
			version : rowData.version
		},
		success : function(data) {
			var j = JSON.parse(data);
			if (j.result = 'success') {
				var roleInfo = JSON.parse(j.msg);
				var editSystemInfoDiv = initEditWindow(roleInfo);
				editSystemInfoDiv.window("open");
			} else {
				$.messager.alert('通知', j.msg,'warn');
				$("#gridTable").datagrid("reload");
			}
		}
	});
}

/**
 * 初始化修改窗口
 */
function initEditWindow(roleInfo){
	   return $("#editDiv").window({
			onBeforeOpen:function(){
				$("#roleId_edit").val(roleInfo.roleId);
				$("#roleName_edit").val(roleInfo.roleName);
				$("#roleDesc_edit").val(roleInfo.roleDesc);
				$("#version_edit").val(roleInfo.version);
				$.ajax({
					url : '<%=request.getContextPath()%>/roleInfo/roleInfoAction!initRoleResource.do',
					type : 'post',
					dataType : 'text',
					data : {
						roleId:roleInfo.roleId
					},
					cache : false,
					success : function(data) {
						$("#role_resource_edit").tree('loadData',eval(data));
					}
				});
			},
			onBeforeClose:function(){
				
			}
		});
}

/**
 * 修改窗口提交
 */
function editWindowSubmit() {
	$.ajax({
		url : '<%=request.getContextPath()%>/roleInfo/roleInfoAction!editRoleInfo.do',
		type : 'post',
		dataType : 'text',
		cache : false,
		data : {
			roleId:$("#roleId_edit").val(),
			roleName:$("#roleName_edit").val(),
			roleDesc:$("#roleDesc_edit").val(),
			version:$("#version_edit").val(),
			roleFuncs:getRoleFuncs()
		},
		success : function(data) {
			var j = JSON.parse(data);
			if (j.result = 'success') {
				$.messager.alert('操作结果', '角色信息修改成功！', 'info');
				editWindowClose();
				$("#gridTable").datagrid("reload");
			} else {
				$.messager.alert('操作结果', j.msg, 'error');
			}
		}
	});
}

/**
 * 获取角色资源
 */
function getRoleFuncs(){
	var result = '';
	var funcs = $("#role_resource_edit").tree("getChecked");
	if(funcs.length > 0){
		for(var i=0;i<funcs.length;i++){
			var funcCode = funcs[i].id;
			if(result == ''){
				result = funcCode;
			}else{
				result += "," + funcCode;
			}
		}
	}
	return result;
}

/**
 * 关闭修改窗口
 */
function editWindowClose() {
	$("#editDiv").window("close");
}

/**
 * 查看
 */
function view(){
	var rowData = $('#gridTable').datagrid('getSelected');
	if(rowData == null){
		messageAlertWarning("操作提示","请选择要查看的角色信息！");
		return;
	}
	$.ajax({
		url : '<%=request.getContextPath()%>/roleInfo/roleInfoAction!initEditRoleInfo.do',
		type : 'post',
		dataType : 'text',
		cache : false,
		data : {
			roleId : rowData.roleId,
			version : rowData.version
		},
		success : function(data) {
			var j = JSON.parse(data);
			if (j.result = 'success') {
				var roleInfo = JSON.parse(j.msg);
				var editSystemInfoDiv = initViewWindow(roleInfo);
				editSystemInfoDiv.window("open");
			} else {
				$.messager.alert('通知', j.msg,'warn');
				$("#gridTable").datagrid("reload");
			}
		}
	});
}

/**
 * 初始化查看窗口
 */
function initViewWindow(roleInfo){
	   return $("#viewDiv").window({
			onBeforeOpen:function(){
				$("#roleId_view").text(roleInfo.roleId);
				$("#roleName_view").text(roleInfo.roleName);
				$("#roleDesc_view").text(roleInfo.roleDesc);
				var roleStatusNameFormat = "";
				if(roleInfo.roleStatus == '0'){
					roleStatusNameFormat = "<span style='color:green;'>"+roleInfo.roleStatusName+"</span>";
				}else if(roleInfo.roleStatus == '1'){
					roleStatusNameFormat = "<span style='color:red;'>"+roleInfo.roleStatusName+"</span>";
				}
				$("#roleStatusName_view").html(roleStatusNameFormat);
				$("#recordCrtOperLoginName_view").text(roleInfo.recordCrtOperLoginName);
				$("#recordCrtTime_view").text(roleInfo.recordCrtTime);
				$("#recordUpdOperLoginName_view").text(roleInfo.recordUpdOperLoginName);
				$("#recordUpdTime_view").text(roleInfo.recordUpdTime);
				$("#version_view").text(roleInfo.version);
				$.ajax({
					url : '<%=request.getContextPath()%>/roleInfo/roleInfoAction!initRoleResource.do',
					type : 'post',
					dataType : 'text',
					data : {
						roleId:roleInfo.roleId
					},
					cache : false,
					success : function(data) {
						$("#role_resource_view").tree('loadData',eval(data));
					}
				});
			},
			onBeforeClose:function(){
				
			}
		});
}

/**
 * 关闭查看窗口
 */
function viewWindowClose() {
	$("#viewDiv").window("close");
}

/**
 * 删除用户信息
 */
function remove(){
	var rowData = $('#gridTable').datagrid('getSelected');
	if(rowData == null){
		messageAlertWarning("操作提示","请选择要删除的角色信息！");
		return;
	}
	messageConfirm("操作提示",'删除角色信息会删除相关的资源信息(不可恢复),确定要删除吗?',removeCallback);
}

/**
 * 删除操作回调函数
 */
function removeCallback(){
	var rowData = $('#gridTable').datagrid('getSelected');
	$.ajax({
		url : '<%=request.getContextPath()%>/roleInfo/roleInfoAction!deleteRoleInfo.do',
		type : 'post',
		dataType : 'text',
		cache : false,
		data : {
			roleId : rowData.roleId,
			version : rowData.version
		},
		success : function(data) {
			var j = JSON.parse(data);
			if (j.result = 'success') {
				$.messager.alert('操作结果', '角色信息删除成功！','info');
				$("#gridTable").datagrid("reload");
			} else {
				$.messager.alert('操作结果', j.msg,'error');
			}
		}
	});
}
</script>
</head>

<body style="background: white;" >
	<div id="searchPanel" style="width:'100%';height:95px;padding:20px;" align="center">
		<table>
			<tr>
				<td>角色名称：<input id="roleName_search" type="text" style="width:100px"></td>
				<td>角色描述：<input id="roleDesc_search" type="text" style="width:100px"></td>
			</tr>
			<tr>
				<td colspan="2" align="center" style="padding-top: 10px">
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" onclick="roleInfoSearch()">查询</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" onclick="clearSearchCriteria()">清空</a>
				</td>
			</tr>
		</table>
	</div>
	
	<!-- 工具栏 -->
	<div id="toolbar" style="padding:5px;">
		<a href="javascript:void(0)" id="editBtn" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'" onclick="edit()">修改</a>
		<a href="javascript:void(0)" id="removeBtn" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'" onclick="remove()">删除</a>
		<a href="javascript:void(0)" id="viewBtn" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" onclick="view()">查看</a>
	</div>
	
	<!-- 数据Table -->
	<table id="gridTable" style="width:'90%';height:'100%'">
	</table>
	
	<!-- 编辑窗口 -->
	<div id="editDiv" style="padding:10px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding:10px;padding-left:80px">
		    	<table>
		    		<input type="hidden" id="roleId_edit"/>
		    		<input type="hidden" id="version_edit"/>
	    			<tr>
		    			<td>角色名称：</td>
		    			<td>
		    				<input type="text" id="roleName_edit"/>
		    			</td>
		    		</tr>
		    		<tr>
		    			<td>角色描述：</td>
		    			<td>
		    				<input type="text" id="roleDesc_edit"/>
		    			</td>
		    		</tr>
		    		<tr>
		    			<td>角色资源：</td>
		    			<td>
		    				<div style="height:240px;width:250px;overflow-y:auto;overflow-x:auto;" align="left">
		    					<ul id="role_resource_edit" class="easyui-tree" data-options="animate:true,checkbox:true,cascadeCheck:true" style="width:220px;"></ul>
		    				</div>
		    			</td>
		    		</tr>
		    	</table>
			</div>
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="editWindowSubmit()">提交</a>
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="editWindowClose()">取消</a>
			</div>
		</div>
	</div>
	
	<!-- 查看窗口 -->
	<div id="viewDiv" style="padding:10px;">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding:10px;" align="center">
		    	<table class="view_table">
		    		<tr>
		    			<th>角色ID</th>
		    			<td id="roleId_view"/>
		    			<th>角色名称</th>
		    			<td id="roleName_view"/>
		    		</tr>
		    		<tr>
		    			<th>角色描述</th>
		    			<td id="roleDesc_view"/>
		    			<th>角色状态</th>
		    			<td id="roleStatusName_view"/>
		    		</tr>
		    		<tr>
		    			<th>记录创建人</th>
		    			<td id="recordCrtOperLoginName_view"/>
		    			<th>记录创建时间</th>
		    			<td id="recordCrtTime_view"/>
		    		</tr>
		    		<tr>
		    			<th>记录更新人</th>
		    			<td id="recordUpdOperLoginName_view"/>
		    			<th>记录更新时间</th>
		    			<td id="recordUpdTime_view"/>
		    		</tr>
		    		<tr>
		    			<th>版本号</th>
		    			<td id="version_view"/>
		    			<th></th>
		    			<td></td>
			    	</tr>
		    		<tr>
		    			<th>角色资源</th>
		    			<td colspan="3" >
		    				<div style="height:180px;width:280px;overflow-y:auto;overflow-x:auto;" align="left">
		    					<ul id="role_resource_view" class="easyui-tree" data-options="animate:true,checkbox:true,cascadeCheck:true" style="width:240px;"></ul>
		    				</div>
		    			</td>
		    		</tr>
		    	</table>
			</div>
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="viewWindowClose()">关闭</a>
			</div>
		</div>
	</div>
</body>
</html>